﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class Admin_upBill : System.Web.UI.Page
{
    AccessDAO dao = new AccessDAO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //Session["picture"] = "";
            int id = Convert.ToInt32(Session["billId"].ToString());
            string sql = "select * from billInfo where id=" + id;
            DataSet ds = dao.getDs(sql);
            foreach (DataRow dr in ds.Tables["message"].Rows)
            {
                this.lblName.Text = dr["name"].ToString();
                Session["picture"] = dr["picture"].ToString();
            }
        }
    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
        Server.Transfer("billInfo.aspx");
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Session["billId"].ToString());
        string picture = this.FileUpload.PostedFile.FileName;
        string fileName = "images/" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

        if (!picture.Equals(""))
        {
            if (Path.GetExtension(picture).Equals(".gif") || Path.GetExtension(picture).Equals(".jpg") || Path.GetExtension(picture).Equals(".jpeg") || Path.GetExtension(picture).Equals(".png"))
            {
                fileName += Path.GetExtension(picture);
                string sql = "update billInfo set picture='" + fileName + "' where id=" + id;
                if (dao.getComm(sql) != 0)
                {
                    this.FileUpload.PostedFile.SaveAs(Server.MapPath("~/") + "\\" + fileName);
                    RegisterStartupScript("check", "<script language='javascript'>alert('更新成功');</script>");

                    Server.Transfer("billInfo.aspx");
                }
                else
                {
                    RegisterStartupScript("check", "<script language='javascript'>alert('更新失败');</script>");
                }

            }
            else
            {
                RegisterStartupScript("check", "<script language='javascript'>alert('将图片格式改为*.jpg或*.gif或*.jpeg或*.png');</script>");
            }
        }
        else
        {
            string sql1 = "update billInfo set picture='" + Session["picture"].ToString() + "' where id=" + id;
            if (dao.getComm(sql1) != 0)
            {
                RegisterStartupScript("check", "<script language='javascript'>alert('更新成功');</script>");

                Server.Transfer("billInfo.aspx");
            }
            else
            {
                RegisterStartupScript("check", "<script language='javascript'>alert('更新失败');</script>");
            }

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Server.Transfer("billInfo.aspx");
    }
}
